home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / passwd.c < prev    next >
C/C++ Source or Header  |  1990-07-19  |  2KB  |  105 lines

  1. /* passwd - change a passwd        Author: Adri Koppes */
  2.  
  3. #include <sys/types.h>
  4. #include <fcntl.h>
  5. #include <string.h>
  6. #include <signal.h>
  7. #include <pwd.h>
  8.  
  9. char pwd_file[] = "/etc/passwd";
  10. char pw_tmp[] = "/etc/pwtemp";
  11. char bad[] = "Permission denied\n";
  12. char buf[512];
  13.  
  14. main(argc, argv)
  15. int argc;
  16. char *argv[];
  17. {
  18.   int uid, cn, n;
  19.   int fpin, fpout;
  20.   long salt;
  21.   struct passwd *pwd, *getpwnam(), *getpwuid(), *getpwent();
  22.   char name[9], password[14], sl[2];
  23.   char *getpass(), *crypt(), *itoa();
  24.  
  25.   uid = getuid();
  26.  
  27.   if (!access(pw_tmp, 0)) {
  28.     std_err("Temporary file in use.\nTry again later\n");
  29.     exit(1);
  30.   }
  31.   if (argc < 2) {
  32.     pwd = getpwuid(uid);
  33.     strcpy(name, pwd->pw_name);
  34.   } else {
  35.     strcpy(name, argv[1]);
  36.     pwd = getpwnam(name);
  37.   }
  38.   if (!pwd || ((uid != pwd->pw_uid) && uid)) {
  39.     std_err(bad);
  40.     exit(1);
  41.   }
  42.   signal(SIGHUP, SIG_IGN);
  43.   signal(SIGINT, SIG_IGN);
  44.   signal(SIGQUIT, SIG_IGN);
  45.   signal(SIGTERM, SIG_IGN);
  46.   prints("Changing password for %s\n", name);
  47.   if (pwd->pw_passwd[0] && uid)
  48.     if (strcmp(pwd->pw_passwd, crypt(getpass("Old password: "), pwd->pw_passwd))) {
  49.         std_err(bad);
  50.         exit(1);
  51.     }
  52.   strcpy(password, getpass("New password: "));
  53.   if (password[0] == '\0') {
  54.     std_err("password cannot be null\n");
  55.     exit(1);
  56.   }
  57.   if (strcmp(password, getpass("Retype password: "))) {
  58.     std_err("Passwords don't match\n");
  59.     exit(1);
  60.   }
  61.   time(&salt);
  62.   sl[0] = (salt & 077) + '.';
  63.   sl[1] = ((salt >> 6) & 077) + '.';
  64.   for (cn = 0; cn < 2; cn++) {
  65.     if (sl[cn] > '9') sl[cn] += 7;
  66.     if (sl[cn] > 'Z') sl[cn] += 6;
  67.   }
  68.   if (password[0]) strcpy(password, crypt(password, sl));
  69.   umask(0);
  70.   close(1);
  71.   fpout = creat(pw_tmp, 0600);
  72.   if (fpout != 1) {
  73.     std_err("Can't create temporary file\n");
  74.     exit(1);
  75.   }
  76.   setpwent();
  77.   while ((pwd = getpwent()) != 0) {
  78.     if (!strcmp(name, pwd->pw_name)) pwd->pw_passwd = password;
  79.     prints("%s:%s:%s:", pwd->pw_name, pwd->pw_passwd, itoa(pwd->pw_uid));
  80.     prints("%s:%s:%s:%s\n", itoa(pwd->pw_gid), pwd->pw_gecos, pwd->pw_dir,
  81.            pwd->pw_shell);
  82.   }
  83.   endpwent();
  84.   close(0);
  85.   if ((fpin = open(pw_tmp, 0)) != 0) {
  86.     std_err("Can't reopen temporary file\n");
  87.     exit(1);
  88.   }
  89.   close(fpout);
  90.   if ((fpout = open(pwd_file, O_RDWR)) < 0) {
  91.     std_err("Can't recreate password file\n");
  92.     unlink(pw_tmp);
  93.     exit(1);
  94.   }
  95.   while (1) {
  96.     n = read(fpin, buf, 512);
  97.     if (n <= 0) break;
  98.     write(1, buf, n);
  99.   }
  100.  
  101.   close(fpin);
  102.   close(fpout);
  103.   unlink(pw_tmp);
  104. }
  105.